package com.knife.lov.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.knife.cache.redis.constants.RedisConstant;
import com.knife.lov.domain.entity.KnifeCodeValue;
import com.knife.lov.infra.cache.v2.KnifeCodeCache;
import com.knife.lov.infra.cache.v2.KnifeCodeValueCache;
import com.knife.lov.infra.mapper.KnifeCodeMapper;
import com.knife.lov.infra.mapper.KnifeCodeValueMapper;
import com.knife.redis.KnifeRedisHelper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * Created with IntelliJ IDEA.
 *
 * @author： 76875
 * @date： 2022/4/15 星期五 18:00
 * @description：
 * @modifiedBy：
 * @version: 1.0
 */
@Configuration("KnifeLovRedisConfigure")
public class KnifeLovRedisConfigure {

    @Resource
    private ObjectMapper objectMapper;
    @Resource
    private KnifeRedisHelper knifeRedisHelper;
    @Resource
    private KnifeCodeMapper knifeCodeMapper;
    @Resource
    private KnifeCodeValueMapper knifeCodeValueMapper;

    @Bean
    public KnifeCodeCache knifeCodeCache() {
        return new KnifeCodeCache(3,
                RedisConstant.LOV + ":" + "codeCache",
                () -> knifeCodeMapper.selectList(null),
                t -> t.getTenantId() + ":" + t.getCode(),
                knifeRedisHelper,
                objectMapper
        );
    }

    @Bean
    public KnifeCodeValueCache knifeCodeValueCache() {
        return new KnifeCodeValueCache(3,
                RedisConstant.LOV + ":" + "codeValueCache",
                () -> knifeCodeValueMapper.selectList(null),
                t -> t.getTenantId() + ":" + t.getCodeId(),
                KnifeCodeValue::getValue,
                knifeRedisHelper,
                objectMapper
        );
    }

}
